package com.zzh.takeoutapp.activity;

import android.content.Intent;
import android.support.v7.widget.AppCompatEditText;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.zzh.takeoutapp.R;
import com.zzh.takeoutapp.adapter.OrderShopAdapter;
import com.zzh.takeoutapp.base.BaseActivity;
import com.zzh.takeoutapp.event.NetMsgEvent;
import com.zzh.takeoutapp.model.AddressBean;
import com.zzh.takeoutapp.model.OrderShopBean;
import com.zzh.takeoutapp.service.EventbusHelper;
import com.zzh.takeoutapp.service.ServiceUrl;
import com.zzh.takeoutapp.utils.DividerGridItemDecoration;
import com.zzh.takeoutapp.utils.GsonResolve;
import com.zzh.takeoutapp.utils.LoadingUtil;
import com.zzh.takeoutapp.utils.StringUtils;
import com.zzh.takeoutapp.utils.ToolBarHelper;
import com.zzh.takeoutapp.utils.UtilTool;

import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.text.ParseException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by Administrator on 2018/8/4.
 */

public class SubmitOrderActivity extends BaseActivity {

    @Bind(R.id.rl_address)
    RelativeLayout rl_address;
    @Bind(R.id.ev_into)
    AppCompatEditText ev_into;
    @Bind(R.id.tv_online_pay)
    TextView tv_online_pay;
    @Bind(R.id.tv_min_price)
    TextView tv_min_price;
    @Bind(R.id.tv_shop_title)
    TextView tv_shop_title;
    @Bind(R.id.tv_package_price)
    TextView tv_package_price;
    @Bind(R.id.tv_freight_stage)
    TextView tv_freight_stage;
    @Bind(R.id.tv_support_hongbao)
    TextView tv_support_hongbao;
    @Bind(R.id.tv_youhui)
    TextView tv_youhui;
    @Bind(R.id.tv_money)
    TextView tv_money;
    @Bind(R.id.sub_youhui)
    TextView sub_youhui;
    @Bind(R.id.sub_money)
    TextView sub_money;
    @Bind(R.id.tv_submit_order)
    TextView tv_submit_order;
    @Bind(R.id.toolbar)
    Toolbar tbToolbar;
    @Bind(R.id.tv_shop_addr)
    TextView tv_shop_addr;//地址
    @Bind(R.id.tv_contact)
    TextView tv_contact;//姓名
    @Bind(R.id.tv_mobile)
    TextView tv_mobile;//电话
    @Bind(R.id.recyclerView)
    RecyclerView recyclerView;
    private List<OrderShopBean> beans;
    private OrderShopAdapter shopAdapter;
    public static final String SUBMIT_ORDER = "submit_order";//提交订单
    public static final String CREATE_ORDER = "CreateOrder";//生成订单
    private String Shop_id;
    private String buffer;
    private String online_pay = "";//支付状态
    private String product_price = "";//金额
    private String min_price = "";//时间
    private String coupon_id = "";
    private String hongbao_id = "";
    private String addr_id = "";
    private String products = "";
    private String total_price = "";
    private String shop_title = "";
    private String shop_mobile = "";
    private String lat = "";
    private String lng = "";
    private LoadingUtil loadingUtil;

    @Override
    public void setContent() {
        setContentView(R.layout.activity_submit_order);
        ButterKnife.bind(this);
    }

    @Override
    public void initComponent() {
        loadingUtil = new LoadingUtil(this);
        tbToolbar.setBackgroundColor(getResources().getColor(R.color.yellow));
        ToolBarHelper.setTitleAndBackAndText(this, tbToolbar, "提交订单");
        Shop_id = getIntent().getStringExtra("Shop_id");
        buffer = getIntent().getStringExtra("buffer");
        if (Shop_id != null && buffer != null)
            ServiceUrl.OrderServiceUrl(this, SUBMIT_ORDER, Shop_id, buffer);
    }

    @Override
    public void setListener() {
        beans = new ArrayList<>();
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        recyclerView.addItemDecoration(new DividerGridItemDecoration(this, 1, R.color.font_black_6));
    }

    @Override
    public void onAfterInit() {

    }

    @Override
    public void onStart() {
        super.onStart();
        EventbusHelper.register(this);
    }

    @Override
    public void onStop() {
        EventbusHelper.unregister(this);
        loadingUtil.stopShowLoading();
        super.onStop();
    }


    @OnClick({R.id.tv_submit_order, R.id.rl_address})
    @Override
    public void onClick(View view) {
        super.onClick(view);
        switch (view.getId()) {
            case R.id.tv_submit_order:
                loadingUtil.startShowLoading();
                ServiceUrl.CreateOrderServiceUrl(this
                        , online_pay
                        , Shop_id
                        , product_price
                        , ""
                        , ""
                        , coupon_id
                        , hongbao_id
                        , addr_id
                        , total_price
                        , ev_into.getText().toString()
                        , products
                        , lat
                        , lng
                        , CREATE_ORDER);
                break;
            case R.id.rl_address:
                Intent intent = new Intent(this, AddressActivity.class);
                intent.setType("submit_order");
                start_ActivityForResult(intent, 1);
                break;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 1 && data != null) {
            AddressBean addressBean = (AddressBean) data.getSerializableExtra("addressBean");
            setTextData(tv_shop_addr, addressBean.getAddr());
            setTextData(tv_contact, addressBean.getContact());
            setTextData(tv_mobile, addressBean.getMobile());
            lat = addressBean.getLat();
            lng = addressBean.getLng();
            addr_id = addressBean.getAddr_id();
        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onMsgEvent(NetMsgEvent event) {
        String _msgType = (String) event.getMsgType();
        JSONObject jsonObject = event.getMsgJSONObj();
        switch (_msgType) {
            case SUBMIT_ORDER:
                initData(jsonObject);
                break;
            case CREATE_ORDER:
                loadingUtil.stopShowLoading();
                if (jsonObject != null)
                    if (jsonObject.optString("error").equals("0")) {
                        Log.e("jsonObject", jsonObject.toString());
                        JSONObject object = jsonObject.optJSONObject("data");
                        Intent intent = new Intent(this, PayOrderActivity.class);
                        intent.putExtra("shop_title", shop_title);
                        intent.putExtra("shop_mobile", shop_mobile);
                        intent.putExtra("dateline", object.optString("re_time"));
                        intent.putExtra("order_id", object.optString("order_id"));
                        intent.putExtra("pay_status", object.optString("pay_status"));
                        intent.putExtra("online_pay", object.optString("online_pay"));
                        JSONObject jsonObject1 = object.optJSONObject("member");
                        intent.putExtra("uid", jsonObject1.optString("order_id"));
                        intent.putExtra("money", jsonObject1.optString("money"));
                        start_Activity(intent);
                    }
                break;
        }
    }

    private void setTextData(TextView textData, String s) {
        textData.setText(s);
    }


    private void initData(JSONObject jsonObject) {
        if (jsonObject != null)
            if (jsonObject.optString("error").equals("0")) {
                JSONObject data = jsonObject.optJSONObject("data");
                JSONObject m_addr = data.optJSONObject("m_addr");
                online_pay = data.optString("online_pay");
                product_price = data.optString("product_price");
                coupon_id = data.optString("coupon_id");
                hongbao_id = data.optString("hongbao_id");
                products = data.optString("products");
                total_price = data.optString("products");
                min_price = m_addr.optString("min_price");
                addr_id = m_addr.optString("addr_id");
                shop_title = data.optString("shop_title");
                shop_mobile = data.optString("shop_mobile");
                lat = m_addr.optString("lat");
                lng = m_addr.optString("lng");
                setTextData(tv_shop_title, shop_title);
                setTextData(tv_online_pay, online_pay);
                setTextData(tv_package_price, data.optString("tv_package_price"));
                setTextData(tv_freight_stage, data.optString("freight_stage"));
                setTextData(tv_support_hongbao, data.optString("support_hongbao"));
                setTextData(tv_youhui, "已优惠￥" + data.optString("support_youhui"));
                setTextData(sub_youhui, "已优惠￥" + data.optString("support_youhui"));
                setTextData(tv_money, "合计￥" + product_price);
                setTextData(sub_money, "合计￥" + product_price);
                if (data.optString("online_pay").equals("1")) {
                    setTextData(tv_online_pay, "在线支付");
                } else {
                    setTextData(tv_online_pay, "余额支付");
                }
                setTextData(tv_shop_addr, m_addr.optString("addr"));
                setTextData(tv_contact, m_addr.optString("contact"));
                setTextData(tv_mobile, m_addr.optString("mobile"));
                setTextData(tv_min_price, "(大约" + min_price + "分钟送出)");

                JSONArray product_lists = data.optJSONArray("product_lists");
                for (int i = 0; i < product_lists.length(); i++) {
                    try {
                        JSONObject jsonObject1 = product_lists.optJSONObject(i);
                        OrderShopBean bean = GsonResolve.jsonString2Bean(jsonObject1.toString(), OrderShopBean.class);
                        beans.add(bean);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
                shopAdapter = new OrderShopAdapter(beans, this);
                recyclerView.setAdapter(shopAdapter);
                shopAdapter.notifyDataSetChanged();
            }
    }

}
